<?php

namespace App\Admin\Controllers;

use App\Models\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Http\Request;

class UserController extends AdminController
{

    public function users(Request $request)
    {
        $q = $request->get('q');
        return User::where('nickname', 'like', "%$q%")->paginate(null, ['id', 'nickname as text']);
    }

    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '用户';

    private function __($key)
    {
        return [
            'Id' => 'ID',
            'Openid' => 'Openid',
            'Nickname' => '用户名',
            'Avatarurl' => '头像',
            'Created at' => '注册时间',
            'Updated at' => '最后登录时间',
            'Data' => 'Data',
            'Mobile' => '手机号',
            'Balance' => '余额',
            'Is vip' => 'VIP',
            'Vip start' => 'VIP 开始时间',
            'Vip end' => 'VIP 到期时间',
        ][$key] ?? $key;
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new User());

        $grid->disableCreateButton();
        $grid->disableFilter();
        $grid->disableExport();
        $grid->disableRowSelector();
        $grid->disableColumnSelector();

        $grid->actions(function ($actions) {

            // 去掉删除
            $actions->disableDelete();

            // 去掉编辑
            $actions->disableEdit();
        });


        $grid->column('id', $this->__('Id'))->filter();
        $grid->column('avatarurl', $this->__('Avatarurl'))->image('/storage', 50, 50);
        $grid->column('nickname', $this->__('Nickname'))->filter('like');
        $grid->column('mobile', $this->__('Mobile'))->filter();
        $grid->column('balance', $this->__('Balance'))->sortable();
        $grid->column('is_vip', $this->__('Is vip'))->icon([
            0 => 'toggle-off',
            1 => 'toggle-on',
        ])->filter([
            0 => '普通用户',
            1 => 'VIP',
        ]);
        $grid->column('vip_start', $this->__('Vip start'))->display(function($created) {
            return !empty($created) ? date('Y-m-d H:i:s', strtotime($created)) : '--';
        })->sortable();
        $grid->column('vip_end', $this->__('Vip end'))->display(function($created) {
            return !empty($created) ? date('Y-m-d H:i:s', strtotime($created)) : '--';
        })->sortable();
        $grid->column('created_at', $this->__('Created at'))->display(function($created) {
            return date('Y-m-d H:i:s', strtotime($created));
        })->sortable();
        $grid->column('updated_at', $this->__('Updated at'))->display(function($created) {
            return date('Y-m-d H:i:s', strtotime($created));
        })->sortable();
        // $grid->column('data', $this->__('Data'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(User::findOrFail($id));

        $show->panel()
        ->tools(function ($tools) {
            $tools->disableEdit();
            $tools->disableDelete();
        });

        // $show->field('id', $this->__('Id'));
        // $show->field('openid', $this->__('Openid'));
        $show->field('avatarurl', $this->__('Avatarurl'))->image('/storage', 100, 100);
        $show->field('nickname', $this->__('Nickname'));
        // $show->field('data', $this->__('Data'));
        $show->field('mobile', $this->__('Mobile'));
        $show->field('balance', $this->__('Balance'));
        $show->field('is_vip', $this->__('Is vip'))->using([0 => '普通用户', 1 => 'VIP用户']);
        $show->field('vip_start', $this->__('Vip start'))->display(function($created) {
            return !empty($created) ? date('Y-m-d H:i:s', strtotime($created)) : '';
        });
        $show->field('vip_end', $this->__('Vip end'))->display(function($created) {
            return !empty($created) ? date('Y-m-d H:i:s', strtotime($created)) : '';
        });
        $show->field('created_at', $this->__('Created at'))->display(function($created) {
            return date('Y-m-d H:i:s', strtotime($created));
        });
        $show->field('updated_at', $this->__('Updated at'))->display(function($created) {
            return date('Y-m-d H:i:s', strtotime($created));
        });

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new User());

        $form->text('openid', $this->__('Openid'));
        $form->text('nickname', $this->__('Nickname'));
        $form->text('avatarurl', $this->__('Avatarurl'));
        $form->text('data', $this->__('Data'));
        $form->mobile('mobile', $this->__('Mobile'));
        $form->decimal('balance', $this->__('Balance'))->default(0.00);
        $form->switch('is_vip', $this->__('Is vip'));
        $form->datetime('vip_start', $this->__('Vip start'))->default(date('Y-m-d H:i:s'));
        $form->datetime('vip_end', $this->__('Vip end'))->default(date('Y-m-d H:i:s'));

        return $form;
    }
}
